package com.nameli.admin.service.impl.system;

import com.nameli.SIM.bean.entity.sys.SysUser;
import com.nameli.admin.config.SecurityTools;
import com.nameli.admin.service.SysMenuService;
import com.nameli.admin.service.SysRoleService;
import org.aspectj.lang.annotation.Around;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Set;

/**
 * @author NAME-L
 * @Description 用户权限处理
 * @className SysPermissionService
 * @date 2022-01-21 09:54:53
 */
@Component
public class SysPermissionService {

    @Autowired
    private SysMenuService menuService;

    @Autowired
    private SysRoleService roleService;


    public Set<String> getRolePermission(SysUser user)
    {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (SecurityTools.isAdmin(user.getUserId()))
        {
            roles.add("admin");
        }
        else
        {
            roles.addAll(roleService.selectRolePermissionByUserId(user.getUserId()));
        }
        return roles;
    }

    /**
     * 获取菜单数据权限
     * @param user 用户信息
     * @return 菜单权限信息
     */
    public Set<String> getMenuPermission(SysUser user) {
        Set<String> perms = new HashSet<String>();
        // 管理员拥有所有权限
        if (SecurityTools.isAdmin(user.getUserId())) {
            perms.add("*:*:*");
        } else {
            perms.addAll(menuService.selectMenuPermsByUserId(user.getUserId()));
        }
        return perms;
    }
}
